<?php
// 应用公共文件

/**
 * 跨域请求头
 */
function headers()
{
    return [
        'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, accesslogin',
        // 'Access-Control-Allow-Origin' => '*'
    ];
}

/**
 * 失败【抛出异常】
 */
function exceptionJson($message = '失败', $code = 400, $data = [])
{
    $json = [
        'meta'    => [
            'status'    => $code,
            'message' => $message,
        ],  
        'data'    => $data
    ];

    return json($json);
}

/**
 * 失败【验证失败】
 */
function validateJson($data = [], $message = '失败', $code = 500)
{
    $json = [
        'meta'    => [
            'status'    => $code,
            'message' => $message,
        ],  
        'data'    => $data
    ];

    return json($json);
}

/**
 * 成功
 */
function successJson($data = [], $message = '成功', $code = 200)
{
    $json = [
        'meta'    => [
            'status'    => $code,
            'message' => $message,
        ],      
        'data'    => $data
    ];

    return json($json);
}

/**
 * 抛出异常
 */
function exception($message = '系统繁忙请稍后再试', $code = 900)
{
    throw new \think\Exception($message, $code);
}

/**
 * 将unicode字符串按传入长度分割成数组
 * @param  string  $str 传入字符串
 * @param  integer $l   字符串长度
 * @return mixed      数组或false
 */
 function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
 }

/**
 * 计算时间
 * @param  ing  $pgm_len  时间
 */
function calculat_time($pgm_len)
{
    $time = gmdate('H:i:s', (int)$pgm_len/25)."-".(int)$pgm_len%25;
    return $time;
}
